<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <link rel="stylesheet" href="../../css/Token_Start.css"/>
    <link rel="stylesheet" href="../../common/common.css"/>
</head>
<body>
<header class="Order_header">
    <strong class="Token_Transaction" id="Append_Header3"><img src="../../img/left.png" alt=""/></strong>
    <span>
      <p>备份钱包</p>
    </span>
</header>
<div class="Back_up">
    <span class="back_up_header">确认你的钱包助记词</span>
    <span class="back_up_text">
     按顺序点击助记词，已确认你的本份助记词正确
    </span>
</div>
<div class="Back_up3">
    <span class="back_up_tet" id="backupTet">
      
    </span>
</div>
<div class="back_up_action"><span class="recall_all" id="recallAll">全部撤回</span><span class="recall_word" id="recallWord">取消</span></div>
<div class="Back_up3">
    <span class="back_up_text3">
        
    </span>
</div>


<div class="Append_content2" id="Back_jump3"> <span>确 认</span>  </div>
<div class="tip-error">错误提示</div>

<script src="../../js/jquery-3.2.1.min.js"></script>
<script>
    $("#Append_Header3").click(function(){
        location.href="Token_append_money2.html"
    })
   
    $().ready(function(){
    	Array.prototype.shuffle = function() {
    	    var array = this;
    	    var m = array.length, t, i;
    	    while (m) {
    	        i = Math.floor(Math.random() * m--);
    	        t = array[m];
    	        array[m] = array[i];
    	        array[i] = t;
    	    }
    	    return array;
    	}
    	var codeAdd=JSON.parse(sessionStorage.getItem("codeAdd"));
    	var txts=null;
    	 $.ajax({
     		url:"/app/coldwallet/querymemorizing",
     		data:codeAdd,
     		type:"post",
     		success:function(r){
     			console.log(r);
     			if(r.code==0){
     				 var memorizing = r.walletEntity.memorizing;
     				memorizing=memorizing.replace("[","");
     				memorizing=memorizing.replace("]","");
     				var txt=memorizing.split(" ");
     				txts=txt.filter(function(age){
     					return age!=="";
     				})
     	    		txts.shuffle();	
     				var backup=""
     	    		for(var j=0,len=txts.length;j<len;j++){
     	    			backup+="<span>"+txts[j]+"</span>&nbsp;&nbsp;"
     	    		}
     				$(".back_up_text3").html(backup);
     				$(".back_up_text3").on("click","span",function(e){
     					if($(".tip-error").css("display")=="block"){
     						$(".tip-error").css("display","none");
     						
     						};
     					if(!$(this).hasClass("back_up_disable")){
         					$(this).addClass("back_up_curr");
         					$(this).siblings(".back_up_curr").removeClass("back_up_curr").addClass("back_up_disable");
         					$("#backupTet").append("  <span>"+$(this).html()+"</span>");
     					}
     				})
     			}else{
     				alert(r.msg);
     			}
     			
     		}
     		}) 
     		//撤回助记词
     		$("#recallWord").on("click",function(){
     			var lastSpan=$(".back_up_tet>span:last");
     			var text=lastSpan.html();
     			var text2=lastSpan.prev().html();
     			$(".back_up_text3 span").map(function(item,index){
     				if($(this).html()==text){
     					$(this).removeClass("back_up_curr");
     				}
     				if($(this).html()==text2){
     					$(this).removeClass("back_up_disable").addClass("back_up_curr");;
     				}
     			})
     			$("#backupTet>span:last").remove();
     		})
     		$("#recallAll").on("click",function(){
     			$("#backupTet").empty();
     			$(".back_up_text3 span").map(function(){
     				if($(this).hasClass("back_up_disable")){
     					$(this).removeClass("back_up_disable");
     				}
     				if($(this).hasClass("back_up_curr")){
     					$(this).removeClass("back_up_curr");
     				}
     			})
     			
     		})
     		//确定提交 button
	     $("#Back_jump3").click(function(){
	    	//助记词
	    	var memorizing=[];
	    	$("#backupTet>span").map(function(){
	    		memorizing.push($(this).html())
	    	})
	    	if(memorizing.length!==txts.length){
	    		$(".tip-error").html("请填写完整助记词！").css("display","block");
	    		return true;
	    	}
	    	var codeAdd=JSON.parse(sessionStorage.getItem("codeAdd"));
	    	console.log(memorizing,codeAdd); 
	    	$.ajax({
				url:"/app/coldwallet/verification",
				type:"POST",
				data:{"memorizing":memorizing.toString(),"address":codeAdd.address,"walletid":codeAdd.walletid},
				success:function(r){
					//console.log(r);
					if(r.code==0){
						location.href="../Token_Assets.html"	
					}else{
						$(".tip-error").html(r.msg).css("display","block");
					}
				}
		})
	        //location.href="../Token_Assets.html"
	    })
     		
     		
    })
</script>

</body>
</html>